<template>
  <div
      style="background-color: #3287ce; width: 100%; height: 100vh; overflow: hidden; display: flex; justify-content: center; align-items: center">
    <div
        style="min-width:700px; display: flex; background-color: white; width: 50%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/register.png" alt="" width="100%">
      </div>
      <div style="flex: 1; display: flex;justify-content: center; align-items: center">
        <el-form :model="user" :rules="rules" ref="registerRef" style="width: 75%">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎注册后台管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="mediunm" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="mediunm" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="repassword">
            <el-input prefix-icon="el-icon-lock" size="mediunm" show-password placeholder="请确认密码"
                      v-model="user.repassword"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="success" style="width: 100%" @click="login">注 册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已经有账号，点击<span style="color: #0f9876; cursor: pointer" @click="$router.push('/login')">登录</span></div>
          </div>

        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "Register",
  data() {
    //密码校验
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码'))
      } else if (value !== this.user.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    return {
      code: '',  //验证码组件的code
      user: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        repassword: [
          {validator: validatePassword, trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    getCode(code) {
      this.code = code.toLowerCase();
    },
    login() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          //验证通过
          this.$request.post('/register', this.user).then(res => {
            if (res.code === '200') {
              this.$router.push('/login')
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          })

        }
      })
    }
  }
}
</script>

<style scoped>

</style>